package org.cbyyx.tc.controller;

import lombok.RequiredArgsConstructor;
import org.cbyyx.tc.annotation.AuthCheck;
import org.cbyyx.tc.common.BaseResponse;
import org.cbyyx.tc.common.ResultUtils;
import org.cbyyx.tc.constant.UserConstant;
import org.cbyyx.tc.enums.ErrorCode;
import org.cbyyx.tc.exception.BusinessException;
import org.cbyyx.tc.exception.ThrowUtils;
import org.cbyyx.tc.model.dto.userRequest.UserLoginRequest;
import org.cbyyx.tc.model.dto.userRequest.UserRegisterRequest;
import org.cbyyx.tc.model.dto.userRequest.UserUpdateRequest;
import org.cbyyx.tc.model.vo.userResponse.UserDetailVO;
import org.cbyyx.tc.model.vo.userResponse.UserVO;
import org.cbyyx.tc.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * 用户管理以及登录
 * 提供用户注册、登录、信息查询和修改等功能
 * @author cbyyx
 */
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;


    /**
     * 获取用户详细信息
     * @param id 用户id
     * @return 用户信息
     * @throws BusinessException 当id为空或小于等于0时抛出参数错误异常
     */
    @GetMapping("/{id}")
    @AuthCheck(role = UserConstant.ADMIN)
    public BaseResponse<UserDetailVO> getUser(@PathVariable Long id) {
        ThrowUtils.throwIf(id == null || id <= 0, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(userService.getUser(id));
    }


    /**
     * 修改用户信息（仅限管理员）
     * @param updateRequest 包含要修改的用户信息
     * @return 修改结果
     * @throws BusinessException 当updateRequest为空时抛出参数错误异常
     */
    @PutMapping()
    @AuthCheck(role = UserConstant.ADMIN)
    public BaseResponse<Boolean> updateUser(@RequestBody @Valid UserUpdateRequest updateRequest) {
        ThrowUtils.throwIf(updateRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(userService.updateUser(updateRequest));
    }


    /**
     * 删除用户（仅限管理员）
     * @param id 要删除的用户ID
     * @return 删除结果
     * @throws BusinessException 当id为空或小于等于0时抛出参数错误异常
     */
    @DeleteMapping("/{id}")
    @AuthCheck(role = UserConstant.ADMIN)
    public BaseResponse<Boolean> deleteUser(@PathVariable Long id) {
        ThrowUtils.throwIf(id == null || id <= 0, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(userService.removeById(id));
    }


    /**
     * 用户登录
     * @param loginRequest 包含登录用户名和密码
     * @param request HTTP请求对象
     * @return 登录结果和用户信息
     * @throws BusinessException 当loginRequest为空时抛出参数错误异常
     */
    @PostMapping("/login")
    public BaseResponse<UserVO> login(@RequestBody @Valid UserLoginRequest loginRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(loginRequest == null, ErrorCode.PARAMS_ERROR,"参数未空");
        HttpSession session = request.getSession();
        // 处理用户登录逻辑
        return ResultUtils.success(userService.userLogin(loginRequest,session));
    }


    /**
     * 用户注册
     * @param registerRequest 包含注册用户信息
     * @return 注册结果
     * @throws BusinessException 当registerRequest为空时抛出参数错误异常
     */
    @PostMapping("/register")
    public BaseResponse<Boolean> register(@RequestBody @Valid UserRegisterRequest registerRequest) {
        // 处理用户注册逻辑
        ThrowUtils.throwIf(registerRequest == null, ErrorCode.PARAMS_ERROR,"参数未空");
        return ResultUtils.success(userService.userRegister(registerRequest));
    }


    /**
     * 获取当前登录用户信息
     * @param request HTTP请求对象
     * @return 当前用户信息
     */
    @GetMapping("/currentUser")
    public BaseResponse<UserVO> getCurrentUser(HttpServletRequest request) {
        HttpSession session = request.getSession();
        return ResultUtils.success(userService.getCurrentUser(session));
    }

    @GetMapping("/logout")
    public BaseResponse<Boolean> logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        ThrowUtils.throwIf(session == null, ErrorCode.PARAMS_ERROR, "未登录");
        session.removeAttribute(UserConstant.USER_LOGIN_STATE);
        return ResultUtils.success(true);
    }

    @PostMapping("/balance/add")
    public BaseResponse<Boolean> addBalance(@RequestParam("amount") Long amount) {
        ThrowUtils.throwIf(amount == null || amount <= 0, ErrorCode.PARAMS_ERROR, "参数错误");
        return ResultUtils.success(userService.addBalance(amount));
    }
}